//
//  YLActionSheetViewCell.swift
//  Swift-Codedase
//
//  Created by xyanl on 2024/4/11.
//  Copyright © 2024 考拉🐨. All rights reserved.
//

import UIKit
import JKSwiftExtension
import Reusable

extension YLActionSheetViewCell: Reusable {}

class YLActionSheetViewCell: UITableViewCell {
    
    // MARK: - Public
    
    var item: YLActionSheetItem? {
        didSet {
            guard let model = item else { return }
            
            titleLabel.text = model.title
            titleLabel.font = model.titleFont
            titleLabel.numberOfLines = model.subtitle.isEmpty ? 0 : 1
            
            subtitleLabel.text = model.subtitle
            subtitleLabel.font = model.subtitleFont
            subtitleLabel.isHidden = model.subtitle.isEmpty
            
            changeColor()
            
        }
    }
    /// 是否自适应暗黑模式
    var adaptiveDarkness: Bool = true
    /// ture: 隐藏线  false: 显示
    var isHideLine: Bool = false {
        didSet {
            lineView.isHidden = isHideLine
        }
    }
    
    // MARK: - Private
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        // 这个属性的意思是根据 UILabel 的宽度来自动适应字体大小。
        // 但要注意的是，这个属性不会让字体变大，只会缩小，所以开始的时候，可以设置字体fontSize大一点。
        label.adjustsFontSizeToFitWidth = true
        return label
    }()
    
    private lazy var subtitleLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        // 这个属性的意思是根据 UILabel 的宽度来自动适应字体大小。
        // 但要注意的是，这个属性不会让字体变大，只会缩小，所以开始的时候，可以设置字体fontSize大一点。
        label.adjustsFontSizeToFitWidth = true
        label.numberOfLines = 1
        return label
    }()
    
    private var lineView: UIView = UIView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.addSubview(titleLabel)
        contentView.addSubview(subtitleLabel)
        contentView.addSubview(lineView)
        lineView.backgroundColor = .black.setAlpha(0.3)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard let model = item else { return }
        
        let margin: CGFloat = 16
        
        if model.subtitle.isEmpty {
            // 没有副标题
            titleLabel.frame = CGRectMake(margin, 0, width - margin*2, height)
            
        } else {
            // 有副标题
            titleLabel.left = margin
            titleLabel.width = width - margin*2
            
            titleLabel.height = model.title.jk.rectHeight(font: titleLabel.font, size: CGSize(width: width - margin*2, height: CGFLOAT_MAX))
            
            subtitleLabel.left = margin
            subtitleLabel.width = width - margin*2
            subtitleLabel.height = model.title.jk.rectHeight(font: subtitleLabel.font, size: CGSize(width: width - margin*2, height: CGFLOAT_MAX))
            
            titleLabel.top = (height - (titleLabel.height + subtitleLabel.height + 4)) / 2
            subtitleLabel.top = CGRectGetMaxY(titleLabel.frame) + 4
            
        }
        
        lineView.frame = CGRectMake(0, height - 0.5, width, 0.5)
        
    }
    
    private func changeColor() {
        guard let model = item else { return }
        
        let isDark = adaptiveDarkness ? Global.isDark : false
        
        backgroundColor = isDark ? UIColor(hex: "#2D2D2D"): model.backgroundColor
        titleLabel.textColor = isDark ? model.titleDarkColor: model.titleColor
        subtitleLabel.textColor = isDark ? model.subtitleDarkColor: model.subtitleColor
        lineView.backgroundColor = isDark ? UIColor(hex: "#1E1E1E"): model.lineColor
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        
        if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
            changeColor()
        }
        
    }
}
